package server;

/**
 * Thread zaduzen za pisanje podataka u stream, tj. slanje podataka od servere ka klijentu.
 * Thread je blokiran (wait()) u getMessage() metodi, sve dok se ne pozove setMessage()
 * metod. Nakon toga, poruka se vraca getMessage() metodom i upisuje se u stream.
 * 
 * @author Uros Krkic
 */
public class ServerThreadWriter extends Thread {

	/**
	 * Referenca na socket konekciju.
	 */
	private ServerSocketConnection connection;
	

	/**
	 * Konstruktor.
	 * @param connection - referenca na socket konekiciju
	 * @author Uros Krkic
	 */
	public ServerThreadWriter(ServerSocketConnection connection) {
		this.connection = connection;
		this.start();
	}

	
	
	@Override
	public void run() {
		try {
			String message;
			
			while ( (message = connection.getMessage()) != null ) {
				connection.writeData(message);
				// TODO obrisati
				System.out.println("RESPONSE = " + message);
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
